package com.example.teaching_server.service;

import com.example.teaching_server.dto.PageDTO;
import com.example.teaching_server.dto.ResourceDTO;
import com.example.teaching_server.dto.ResourceTypeDTO;
import com.example.teaching_server.entity.Resource;
import com.example.teaching_server.entity.ResourceType;
import com.example.teaching_server.repository.ResourceRepository;
import com.example.teaching_server.repository.ResourceTypeRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 资源管理服务
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ResourceService {

    private final ResourceTypeRepository resourceTypeRepository;
    private final ResourceRepository resourceRepository;

    /**
     * 获取所有资源类型
     */
    public List<ResourceTypeDTO> getAllResourceTypes() {
        log.info("获取所有资源类型");

        return resourceTypeRepository.findAllByOrderByCodeAsc().stream()
                .map(this::convertToTypeDTO)
                .collect(Collectors.toList());
    }

    /**
     * 分页获取资源列表
     */
    public PageDTO<ResourceDTO> getResources(String type, int page, int size) {
        log.info("获取资源列表: type={}, page={}, size={}", type, page, size);

        // 验证分页参数
        validatePaginationParams(page, size);

        Pageable pageable = PageRequest.of(page - 1, size);
        Page<Resource> resourcePage = getResourcePage(type, pageable);

        List<ResourceDTO> items = resourcePage.getContent().stream()
                .map(ResourceDTO::new)
                .collect(Collectors.toList());

        return PageDTO.<ResourceDTO>of(
                items,
                resourcePage.getTotalElements(),
                page,
                size
        );
    }

    /**
     * 转换资源类型实体为DTO
     */
    private ResourceTypeDTO convertToTypeDTO(ResourceType type) {
        return ResourceTypeDTO.builder()
                .code(type.getCode())
                .label(type.getLabel())
                .icon(type.getIcon())
                .sortOrder(0)
                .enabled(true)
                .build();
    }

    /**
     * 验证分页参数
     */
    private void validatePaginationParams(int page, int size) {
        if (page < 1) {
            throw new IllegalArgumentException("页码不能小于1");
        }
        if (size < 1 || size > 100) {
            throw new IllegalArgumentException("每页大小必须在1-100之间");
        }
    }

    /**
     * 获取资源分页数据
     */
    private Page<Resource> getResourcePage(String type, Pageable pageable) {
        if (type != null && !type.trim().isEmpty()) {
            return resourceRepository.findByType(type, pageable);
        } else {
            return resourceRepository.findAll(pageable);
        }
    }
}
